//
//  MoboHardViewController.m
//  mxmovie
//
//  Created by lmx on 16/6/16.
//
//

#import "MoboHardViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "MoboPlayerHardView.h"


@interface MoboHardViewController() {
    BOOL _played;
    NSString *_totalTime;
    NSDateFormatter *_dateFormatter;
}


@property (nonatomic ,strong) AVPlayer *player;
@property (nonatomic ,strong) AVPlayerItem *playerItem;
@property (nonatomic ,weak) IBOutlet  MoboPlayerHardView *playerView;
@property (nonatomic ,weak) IBOutlet UIButton *stateButton;
@property (nonatomic ,weak) IBOutlet UILabel *timeLabel;
@property (nonatomic ,strong) id playbackTimeObserver;
@property (nonatomic ,weak) IBOutlet UISlider *videoSlider;
@property (nonatomic ,weak) IBOutlet UIProgressView *videoProgress;


- (IBAction)stateButtonTouched:(id)sender;
- (IBAction)videoSlierChangeValue:(id)sender;
- (IBAction)videoSlierChangeValueEnd:(id)sender;
@end


@implementation MoboHardViewController
@synthesize moboControlStyle;
//@synthesize moboScalingMode = _moboScalingMode;
//@synthesize moboDisplayView;



- (void)monitoringPlayback:(AVPlayerItem *)playerItem {
    
    __weak typeof(self) weakSelf = self;
    self.playbackTimeObserver = [self.playerView.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:NULL usingBlock:^(CMTime time) {
        CGFloat currentSecond = playerItem.currentTime.value/playerItem.currentTime.timescale;// 计算当前在第几秒
        [weakSelf.videoSlider setValue:currentSecond animated:YES];
        NSString *timeString = [self convertTime:currentSecond];
        weakSelf.timeLabel.text = [NSString stringWithFormat:@"%@/%@",timeString,_totalTime];
    }];
}

- (NSString *)convertTime:(CGFloat)second{
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];
    if (second/3600 >= 1) {
        [[self dateFormatter] setDateFormat:@"HH:mm:ss"];
    } else {
        [[self dateFormatter] setDateFormat:@"mm:ss"];
    }
    NSString *showtimeNew = [[self dateFormatter] stringFromDate:d];
    return showtimeNew;
}

- (NSDateFormatter *)dateFormatter {
    if (!_dateFormatter) {
        _dateFormatter = [[NSDateFormatter alloc] init];
    }
    return _dateFormatter;
}


- (void) play{
    
    [self.player play];
}

- (void) pause
{
    [self pause];
}

- (void) seekToTime: (CGFloat) position
{
    CMTime tm;
    tm = CMTimeMake(position, 1);
    
  //  [self seekToTime:tm];
//    self.currentPlaybackTime = (NSTimeInterval)position;
}
- (BOOL) hasSubtitle
{
    return false;
  
}

- (NSUInteger) getSubtitleCount;
{
    return 0;
}

- (NSArray *) selectedSubtitleStream
{
    return NULL;
}



- (NSArray *) GetSubtitleNSArray
{
    return NULL;
}


- (BOOL) setSelectedSubtitleStream:(NSArray *)selected

{
    return false;
}

- (BOOL) hasAudio
{
    return false;
}



- (NSUInteger) getAudioCount
{
    
    return 0;
}


- (NSArray *) AudioStreamNSArray
{
    
    return NULL;
}

- (NSInteger) selectedAudioStream
{
    
    return 0;
}
- (void) setSelectedAudioStream:(NSInteger)selected
{
    
}
- (BOOL) isPlaying
{
    return false;
}



- (CGFloat) getFullTime;
{
    return 0;
}

- (CGFloat) getCurrentTime
{
    return 0;
}


- (void) stop
{
    
}
- (BOOL) isBufferring;
{
    return false;
}

- (CGFloat) getBufferPercent
{
    return 0;
}

@end
